//--------------------------------------------------------------------//
//     此代码由T4模板自动生成										  //
//	   生成时间 2020-11-12 09:12:36 	                              //
//	   描述：代码生成器
//--------------------------------------------------------------------//
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using System.Collections.Generic;
using System.IO;
using System.Data;
using RayD.App.SystemModule;
using RayD.Repository.Entites.SystemModule;
using RayD.Core;
using RayD.Repository;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace RayD.WebApi.Controllers.Admin
{
    public class CodeGeneratorController : BaseAdminController
    {
        #region 构造函数注入
        private bool _isDevelopment;
        private CodeGeneratorApp _CodeGeneratorApp;
        public CodeGeneratorController(IWebHostEnvironment hostingEnvironment, CodeGeneratorApp CodeGeneratorApp)
        {
            _isDevelopment = hostingEnvironment.IsDevelopment();
            this._CodeGeneratorApp = CodeGeneratorApp;
        }
        #endregion

        #region 请求参数

        #endregion

        #region 获取数据
        /// <summary>
        /// 获取当前表所有字段默认配置
        /// </summary>
        /// <param name="tableName">当前表名</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<FieldConfig>> GetDefaultFieldList(string tableName)
        {
            var res = this._CodeGeneratorApp.GetDefaultFieldList(tableName);
            return Success(res);
        }
        #endregion

        #region 实体开发
        /// <summary>
        /// 实体开发 - 代码预览
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        [HttpPost]
        public ResParameter<string> PreviewEntityCode(CodeBaseConfigModel config)
        {
            string res = this._CodeGeneratorApp.EntityCode(config, true, false);
            return Success("响应成功", res);
        }
        /// <summary>
        /// 实体开发 - 代码生成
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        [HttpPut]
        public ResParameter GenerateEntityCode(CodeBaseConfigModel config)
        {
            if (_isDevelopment)
            {
                this._CodeGeneratorApp.EntityCode(config, true, true);
                return Success("创建成功");
            }
            else
            {
                return Fail("该功能只能在开发环境下使用！");
            }
        }
        #endregion

        #region 单表快速开发
        /// <summary>
        /// 单表快速开发 - 代码预览
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        [HttpPost]
        public ResParameter<object> PreviewRepidCode(CodeBaseConfigModel config)
        {
            string entityCode = this._CodeGeneratorApp.EntityCode(config, false, false);
            string appCode = this._CodeGeneratorApp.AppCode(config, false);
            string controllerCode = this._CodeGeneratorApp.ControllerCode(config, false);
            string indexCode = this._CodeGeneratorApp.IndexCode(config, false);
            string formCode = this._CodeGeneratorApp.FormCode(config, false);
            return Success<object>("响应成功", new
            {
                entityCode,
                appCode,
                controllerCode,
                indexCode,
                formCode
            });
        }
        /// <summary>
        /// 单表快速开发 - 代码生成
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        [HttpPut]
        public ResParameter GenerateRepidCode(CodeBaseConfigModel config)
        {
            if (_isDevelopment)
            {
                //写api.js
                var (isSuccess, msg) = this._CodeGeneratorApp.WriteApi(config);
                if (!isSuccess)
                {
                    return Fail(msg);
                }
                //生成模块
                (isSuccess, msg) = this._CodeGeneratorApp.GenerateModule(config);
                if (!isSuccess)
                {
                    return Fail(msg);
                }
                this._CodeGeneratorApp.EntityCode(config, false, true);
                this._CodeGeneratorApp.AppCode(config, true);
                this._CodeGeneratorApp.ControllerCode(config, true);
                this._CodeGeneratorApp.IndexCode(config, true);
                this._CodeGeneratorApp.FormCode(config, true);
                return Success("创建成功");
            }
            else
            {
                return Fail("该功能只能在开发环境下使用！");
            }
        }
        #endregion
    }
}
